<--- %%NOBANNER%% --> printto.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Redirect the output or the log to an external file;                |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| Argument:                                                          |
|    output: where do you want to print your output to;              |
|    log: where do you want to print your log file to;               |
|    Note: output= and log= can take external files;                 |
|    replace: do you want to overwrite the file if there is a file   |
|             with the same name as given;                           |
|-------------<-- End of Files or Arguments Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example:                                                           |
|   %printto(output=d:\temp\wintemp\atrial.lst, log=d:\temp.log);    |
\-------------------<-- End of Files Created-->---------------------*/
%macro printto(output=,log=,replace=);
/*--------------------------------------------\
| Author:  Duo Zhou;                          |
| Created: 6-15-2001 9:32pm;                  |
| Purpose: redirect the output or the log to  |
|          an external file;                  |
\--------------------------------------------*/
%local outputextstart outputext log extstart logext outid logid fileid;
options noxwait noxsync;
x 'Exit';
%let output=%qscan(&output,1,%str(()''"",));
%let log=%qscan(&log,1,%str(()''"",));
%if &output ne %then %do;
   %let outputextstart=%eval(%length(&output)-4);
   %let outputext=%substr(&output,&outputextstart,5); %put --> Note: The output file extension is "&outputext". ;
   %if (not %index(&outputext,.)) %then %do;
      %let output=&output..lst;
   %end;
   %let outid=%sysfunc(fileexist(&output));
   %if &outid %then %do;
      %if (%index(%upcase(&replace),T)) %then %do;
         %sysexec DEL "&output";
        %put --> Note: Deleting the output file "&output". ;
     %end;
   %end;
   %put --> Note: Output is saving to "&output". ;
%end;

%if &log ne %then %do;
   %let logextstart=%eval(%length(&log)-4);
   %let logext=%substr(&log,&logextstart,5); %put --> Note: The log file extension is "&logext". ;
   %if (not %index(&logext,.)) %then %do;
      %let log=&log..log;
   %end;
   %let logid=%sysfunc(fileexist(&log));
   %if &logid %then %do;
      %if (%index(%upcase(&replace),T)) %then %do;
         %sysexec DEL "&log";
        %put --> Note: Deleting the log file "&log". ;
     %end;
   %end;
   %put --> Note: Log is saving to "&log". ;
%end;
proc printto %if &output ne %then %do;
                    print="&output"
              %end;
          %if (&log ne) %then %do;
                log="&log"
          %end;; run;
%mend printto;